class LabelItem:
    """文字标注项数据模型"""
    def __init__(self, text, x, y, font_path, font_size=24, color="#000000", bold=False):
        self.text = text
        self.x = x
        self.y = y
        self.font_path = font_path
        self.font_size = font_size
        self.color = color
        self.bold = bold

    def to_dict(self):
        """转换为字典格式，用于保存"""
        return {
            "text": self.text,
            "x": self.x,
            "y": self.y,
            "font_path": self.font_path,
            "font_size": self.font_size,
            "color": self.color,
            "bold": self.bold
        }

    @staticmethod
    def from_dict(data):
        """从字典创建实例，用于加载"""
        return LabelItem(
            data.get("text", ""),
            data.get("x", 0),
            data.get("y", 0),
            data.get("font_path", None),
            data.get("font_size", 24),
            data.get("color", "#000000"),
            data.get("bold", False)
        )


class LineItem:
    """线条标注项数据模型"""
    def __init__(self, points, color="#000000", width=2, line_type="straight"):
        """
        初始化线条
        points: 点的列表，[(x1,y1), (x2,y2), ...] 直线2个点，折线3个点
        line_type: "straight" 或 "polyline"
        """
        self.points = points if isinstance(points, list) else []
        self.color = color
        self.width = width
        self.line_type = line_type
        
        # 为了兼容旧版本，如果传入的是单独的坐标参数
        if not self.points and len(points) == 4:
            self.points = [(points[0], points[1]), (points[2], points[3])]

    # 为了兼容旧代码，保留旧属性的访问方式
    @property
    def x1(self):
        return self.points[0][0] if self.points else 0
    
    @property
    def y1(self):
        return self.points[0][1] if self.points else 0
    
    @property
    def x2(self):
        return self.points[1][0] if len(self.points) > 1 else 0
    
    @property
    def y2(self):
        return self.points[1][1] if len(self.points) > 1 else 0

    def to_dict(self):
        """转换为字典格式，用于保存"""
        return {
            "points": self.points,
            "color": self.color,
            "width": self.width,
            "line_type": self.line_type,
            # 为了兼容旧版本，保留旧格式
            "x1": self.x1,
            "y1": self.y1,
            "x2": self.x2,
            "y2": self.y2
        }

    @staticmethod
    def from_dict(data):
        """从字典创建实例，用于加载"""
        # 优先使用新格式
        if "points" in data:
            return LineItem(
                data.get("points", []),
                data.get("color", "#000000"),
                data.get("width", 2),
                data.get("line_type", "straight")
            )
        # 兼容旧格式
        else:
            points = [(data.get("x1", 0), data.get("y1", 0)), 
                     (data.get("x2", 0), data.get("y2", 0))]
            return LineItem(
                points,
                data.get("color", "#000000"),
                data.get("width", 2),
                "straight"
            )


class MaskItem:
    """遮罩项数据模型"""
    def __init__(self, x, y, width, height, color="#000000", opacity=0.5):
        """
        初始化遮罩
        x, y: 左上角坐标
        width, height: 遮罩尺寸
        color: 遮罩颜色
        opacity: 透明度 (0.0-1.0)
        """
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.opacity = opacity

    def to_dict(self):
        """转换为字典格式，用于保存"""
        return {
            "x": self.x,
            "y": self.y,
            "width": self.width,
            "height": self.height,
            "color": self.color,
            "opacity": self.opacity
        }

    @staticmethod
    def from_dict(data):
        """从字典创建实例，用于加载"""
        return MaskItem(
            data.get("x", 0),
            data.get("y", 0),
            data.get("width", 100),
            data.get("height", 100),
            data.get("color", "#000000"),
            data.get("opacity", 0.5)
        )
